import { ElMessageBox } from 'element-plus'
import type { MessageBoxType } from 'element-plus'

interface ConfirmOptions {
  title?: string
  content?: string
  confirmText?: string
  cancelText?: string
  type?: MessageBoxType
}

export const publicConfirm = async (
  options: ConfirmOptions = {},
): Promise<boolean> => {
  const {
    title = '提示',
    content = '确认执行该操作？',
    confirmText = '确认',
    cancelText = '取消',
    type = '',
  } = options
  try {
    await ElMessageBox.confirm(content, title, {
      confirmButtonText: confirmText,
      cancelButtonText: cancelText,
      type: type as any,
    })
    return true
  } catch {
    return false
  }
}

export const deepClone = (obj) => {
  if (obj === null || typeof obj !== 'object') {
    return obj
  }
  if (obj instanceof Date) {
    return new Date(obj.getTime())
  }
  if (obj instanceof RegExp) {
    return new RegExp(obj)
  }
  const clone = Array.isArray(obj) ? [] : {}
  // 使用 Object.keys 遍历可枚举属性
  Object.keys(obj).forEach((key) => {
    clone[key] = deepClone(obj[key])
  })
  return clone
}
